﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using ScrumTracker.Model;

namespace ScrumTrackerAjax.Controllers
{
    public class ProductsController : AbstractController
    {
        public ActionResult Get(int id)
        {
            ViewData.Model = Product.Find(id);
            return View("GetProduct");
        }

        [HttpPost]
        public ActionResult Create(Product model)
        {
            if (ModelState.IsValid)
            {
                model = Product.Create(model);
                return PartialView("_ListItem", model);
            }
            else
            {
                Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
                return PartialView("_CreateProductForm", model);
            }
        }

        [HttpPut]
        public ActionResult Update(int id)//, [Bind(Include="IsHidden")] Product model)
        {
            var p = Product.Find(id);
            if (TryUpdateModel<Product>(p))
            {
                //var db = ScrumTrackerEntities.Instance;
                ScrumTrackerEntities.Instance.SaveChanges();
            }
            return new EmptyResult();
        }

        [HttpDelete]
        public ActionResult Delete(int id)
        {
            Product.Delete(id);
            return new EmptyResult();
        }
    }
}
